
import std.stdio:writefln;

import maths.Matrix;
import maths.Vec;
import maths.Quat;

import utils.Singleton;
import Utils;

class Display{
	mixin SingletonMix;

	void initialize(){
		_projection = projectionMatrix(fov, aspect, zNear, zFar);
		_modelview.makeIdentity();
//BUG		view = _modelview * _projection;
		//modelview = mat4.zRot(90);
		//projection = modelview * projection;
	}

	void modelview(vec3 position, quat orientation){
		_modelview = orientation.getMatrix() * mat4.translation(position);
//BUG		view = _modelview * _projection;
	}

	vec2 project(vec3 vert){
		/* http://www.opengl.org/resources/faq/technical/transformations.htm */
		writefln(vert);
//		writefln("Perspective:\n",_projection);
		vec4 ext = vec4(vert.x, vert.y, vert.z, 1);
//		ext = view.xform(ext);
		ext = _modelview.xform(ext);
		writefln(ext);
		assert(ext.w == 1.0);
		assert(ext.z < -zNear);
		assert(ext.z > -zFar);
		ext = _projection.xform(ext);
		assert(ext.w != 0.0);
		ext /= ext.w;
		writefln(ext);
//WTF		assert(ext.x<1 && ext.x >-1 && ext.y<1 && ext.y >-1 && ext.z<1 && ext.z >-1);
		ext *= 20;
		return vec2(ext.x, ext.y);
	}

	float aspect = 0.75;//1.33333;
	float fov = 120;
	float zNear = 1;
	float zFar = 500;
	mat4 _projection;
	mat4 _modelview;
	mat4 view;
}

alias SingletonGetter!(Display) display;
